$(function(){
	// 表格行上色
	$("#my_tabcol tr").mouseover(function() {
			$(this).toggleClass("bcolor");
		}).mouseout(function() {
			$(this).toggleClass("bcolor");
		});

	$("#lovemusic1").click(function() {
		findSongBySort(1);
	});
	$("#lovemusic2").click(function() {
		findSongBySort(2);
	});
	$("#lovemusic3").click(function() {
		findSongBySort(3);
	});
	$("#lovemusic4").click(function() {
		findSongBySort(4);
	});
	$("#lovemusic5").click(function() {
		findSongBySort(5);
	});
	//2内地，3、港台、4日韩，5欧美
	addSongBySort(1,1);

});


function findSongBySort(nation){
	addSongBySort(1,nation);
}
/**
 * 加载专辑
 */
function addSongBySort(pageNum,nation){
	$.get(path + "/song/song/list", {"pageNum":pageNum,"pageSize":10,"nation":nation}, function(result) {
		if (result.code === 200) {
			$("#my_tabcol").empty();
			$("#pageChange").empty();
			$("#pageChange").append('<span class="pageSpan">页数:</span>');
			var list = result.list;
			index=1;
			$(list).each(function() {
				var tr='<tr height="40px;"><td width="33%" id="my_tabcol_td">'+index+'</td><td width="33%"><span>'+this.songName+'</span><a href="/sites/player.html?songid='+this.id+'"><img src="/image/img/mv.jpg"/></a></td><td width="33%"><a href="javascript:play_mp3('+this.mp3Id+')"><img src="/image/img/erji2.jpg" title="播放"/></a><a href="javascript:down_mp3('+this.mp3Id+')"><img src="/image/img/xiazai.jpg" title="下载"/></a><a href=""><img src="/image/img/collect.jpg" title="加入收藏"/></a></td></tr>';
				$("#my_tabcol").append(tr);
				$("#my_tabcol tr:last").data("song", this);
				index++;
			});

			var page = result.page;
			for (var i = 1; i <= page.pageCount; i++) {
				var a='<span class="pageSpan"><a href="javascript:addSongBySort('+i+','+nation+')">'+i+'</a></span>&nbsp;';
				$("#pageChange").append(a);
			}
		} else {
			alert(result.msg);
		}
	});
}

function play_mp3(songid){
	$.get(path+"/system/files/get-one",{"id":songid},function(result){
		if (result.code === 200) {
			var data=result.data;
			$("#audio_mp3").attr("src", data.url);
		} else {
			alert(result.msg);
		}
	});
}

function down_mp3(songid){
	$.get(path+"/system/files/get-one",{"id":songid},function(result){
		if (result.code === 200) {
			var data=result.data;
			const elt = document.createElement('a');
			elt.setAttribute('href', data.url);
			elt.setAttribute('download', data.fileName);
			elt.style.display = 'none';
			document.body.appendChild(elt);
			elt.click();
			document.body.removeChild(elt);
		} else {
			alert(result.msg);
		}
	});
}
